﻿using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace Gaea.MySql
{
    public class GaeaMySqlAdapter : GaeaAdapter
    {
        public override string AppendColumnNameEqualsValue(string columnName)
        {
            return $"{columnName}=@{columnName}";
        }

        public override string InsertCommandText(string name, List<PropertyInfo> columnProperties)
        {
            var stringBuilderColumn = new StringBuilder();
            var stringBuilderProperty = new StringBuilder();

            for (var i = 0; i < columnProperties.Count; i++)
            {
                var property = columnProperties[i];

                stringBuilderColumn.Append(property.Name);
                stringBuilderProperty.AppendFormat("@{0}", property.Name);

                AppendColumnNameEqualsValue(property.Name);
                if (i < columnProperties.Count - 1)
                {
                    stringBuilderColumn.Append(", ");
                    stringBuilderProperty.Append(", ");
                }

            }
            return $"INSERT INTO {name} ({stringBuilderColumn.ToString()}) VALUES({stringBuilderProperty.ToString()});SELECT LAST_INSERT_ID()";
            //return $"INSERT INTO {name} ({stringBuilderColumn.ToString()}) VALUES({stringBuilderProperty.ToString()});SELECT next VALUE FOR MYCATSEQ_GLOBAL";

        }
    }
}
